export const latitude = ref(0)
export const longitude = ref(0)

interface LocationResult {
  latitude: number
  longitude: number
  address?: string
}

/**
 * 监听页面加载
 */
export async function onPageLoad() {
  uni.showLoading({
    title: '加载中',
    mask: true,
  })
  await getLocation()
  uni.hideLoading()
}
/**
 * 获取当前位置
 */
async function getLocation() {
  return new Promise<void>((resolve) => {
    uni.getLocation({
      type: 'gcj02',
      geocode: true,
      success: (res: LocationResult) => {
        latitude.value = res.latitude
        longitude.value = res.longitude
        resolve()
      },
      fail: (err: any) => {
        console.error('定位失败', err)
        uni.showToast({
          title: '获取位置失败',
          icon: 'none',
        })
        resolve() // 即使失败也要 resolve，避免阻塞后续流程
      },
    })
  })
}
